package main

import (
	_ "xiaoercaidan/webPage/routers"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/session"
	"xiaoercaidan/publicResources/config"
)

var globalSessions *session.Manager

func initSession() {
	sessionConfig := &session.ManagerConfig{
		CookieName:"gosessionid",
		EnableSetCookie: true,
		Gclifetime:3600,
		Maxlifetime: 3600,
		Secure: false,
		CookieLifeTime: 3600,
		ProviderConfig: "./tmp",
	}
	globalSessions, _ = session.NewManager("memory", sessionConfig)
	go globalSessions.GC()
}

func init() {

	config.InitConfig("webPage", true)
	initSession()
	beego.InsertFilter("/v1/*", beego.BeforeRouter, logFilter)
	beego.InsertFilter("/*", beego.FinishRouter, FilterUser)
}

var FilterUser = func(ctx *context.Context) {

	sess, _ := globalSessions.SessionStart(ctx.ResponseWriter, ctx.Request)
	var user = sess.Get("User")

	if user == nil && ctx.Request.RequestURI != "/v1/pages/test/login" {
		ctx.Redirect(302, "/v1/pages/test/login")
	}

}



//增加filter函数
func logFilter(ctx *context.Context) {
	//method := ctx.Request.Method
	//header := ctx.Request.URL
	//body := ctx.Request.Body

	//beego.Debug("[dump http] method: ", method, "url: ", header, "body: ", body)
}

func main() {

	beego.Run()

}

